import json

path = 'D:/project/pythonproject/game_and_utils_py1/utils'
default_path_prefix = f'{path}/common_util/input/'
default_path_prefix_out = f'{path}/common_util/output/'


def read_str(path):
    """
    读取字符串
    :param path: 文件路径
    :return:  字符串
    """
    with open(path, 'r', encoding='UTF-8') as file:
        return file.read()


def read_str_by_name(name):
    """
    根据名称读取字符串
    :param name: 文件名称
    :return: 字符串
    """
    return read_str(default_path_prefix + name)


def read_str_data():
    """
    读取字符串数据
    :return: 字符串
    """
    return read_str_by_name('00data.txt')


def read_str_json():
    """
    读取json数据
    :return: 字符串
    """
    return read_str_by_name('01input.json')


def read_str_model():
    """
    读取模型数据
    :return: 字符串
    """
    return read_str_by_name('02model.txt')


def read_json():
    """
    读取json数据
    :return: json对象
    """
    return json.loads(read_str_json())


def read_json_data(spl0='\n', spl1='\t'):
    """
    读取json数据
    :return: json对象
    """
    data_str = read_str_data()
    result = []
    for line in data_str.split(spl0):
        if line != '':
            result.append(line.split(spl1))
    return result


def write_str(str_out, path):
    """
    写入字符串
    :param str_out: 字符串
    :param path:   文件路径
    :return: 无
    """
    with open(path, 'w', encoding='UTF-8') as f3:
        f3.write(str_out)


def write_str_by_name(str_out, name):
    """
    根据名称写入字符串
    :param str_out: 字符串
    :param name: 文件名称
    :return: None
    """
    write_str(str_out, default_path_prefix_out + name)


def write_str_by_type(str_out, type='txt'):
    """
    根据类型写入字符串
    :param str_out: 字符串
    :param type: 文件路径
    :return: None
    """

    if type == 'json':
        write_str_by_name(str_out, '01result.json')
    elif type == 'sql':
        write_str_by_name(str_out, '02result.sql')
    elif type == 'md':
        write_str_by_name(str_out, '03result.md')
    elif type == 'js':
        write_str_by_name(str_out, '04result.js')
    else:
        write_str_by_name(str_out, '00result.txt')


def write_def(str_out):
    """
    写入默认文件
    :param str_out: 字符串
    :return: None
    """
    write_str_by_type(str_out, 'txt')


def write_json(dict_out):
    """
    写入json数据
    :param dict_out: json对象
    :return: None
    """
    write_str_by_type(json.dumps(dict_out, ensure_ascii=False, indent=2), 'json')
